/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Collections.Generic;
using System.Windows.Input;
#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif

namespace Oceanside
{
    public partial class CompPercentsBox : ModalWindow
    {
        public delegate void OkClickedDelegateType(decimal number, bool wasPercent, 
            COMP_ACCOUNT compAccount, List<SALE> sales, bool isEntireCheck);
        public OkClickedDelegateType OkDelegateClicked;

        bool lastPressWasPercent = false;
        int percent = 0;

        private Stack<int> stackOfEnteredInts = new Stack<int>(10);
        private double Total
        {
            get
            {
                double amount = 0;
                int len = stackOfEnteredInts.Count;
                Stack<int> poppedStack = new Stack<int>(stackOfEnteredInts.Count);
                for (int i = 0; i < len; i++)
                {
                    int pop = stackOfEnteredInts.Pop();
                    poppedStack.Push(pop);

                    if (i < 2)
                    {
                        double multiplier = Math.Pow(10, -(2 - i));
                        amount += pop * multiplier;
                    }
                    else if (i == 2)
                    {
                        amount += pop;
                    }
                    else
                    {
                        double divisor = Math.Pow(10, i - 2);
                        amount += pop * divisor;
                    }
                }
                ///Now reload the stack                
                for (int i = 0; i < len; i++)
                {
                    stackOfEnteredInts.Push(poppedStack.Pop());
                }
                return amount;
            }
        }

        private List<SALE> compableSales;
        private COMP_ACCOUNT compReason;
        private bool isEntireCheck;

        /// <summary>
        /// 
        /// </summary>
        public CompPercentsBox(OkClickedDelegateType del, COMP_ACCOUNT compAccount, List<SALE> compableSales, bool isEntireCheck)
            : base()
        {
            PosWindow.MainPosWindow.Topmost = false;
            InitializeComponent();
            OkDelegateClicked = del;
            keypad.SetNumericKeyCallbackDel(new KeyPad.NumericKeyCallbackDelType(NumericKeyPressed_NOTHROW));
            keypad.SetClearKeyCallbackDel(new KeyPad.ClearCallbackDelType(ClearKeyPressed_NOTHROW));
            keypad.SetOkKeyCallbackDel(new KeyPad.OkCallbackDelType(OkKeyPressed_NOTHROW));
            button5.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button10.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button15.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button20.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button25.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button33.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button50.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button75.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            button40.MouseDown += new MouseButtonEventHandler(buttonPercent_MouseDown_NOTHROW);
            this.Closing += new System.ComponentModel.CancelEventHandler(MessageBox_Closing_NOTHROW);
            compReason = compAccount;
            this.compableSales = compableSales;
            this.isEntireCheck = isEntireCheck;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MessageBox_Closing_NOTHROW(object sender, System.ComponentModel.CancelEventArgs e)
        {
            try
            {
                this.Topmost = false;
                ModalDialogIsOpen = false;
                CurrentlyOpenInstance = null;
#if !_DEBUG
                PosWindow.MainPosWindow.Topmost = true;
#endif
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_COMPPERCENT_MESSAGEBOX_CLOSING, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="sender"></param>
        /// <param Name="e"></param>
        void buttonPercent_MouseDown_NOTHROW(object sender, MouseButtonEventArgs e)
        {
            try
            {
                e.Handled = true;
                ShineButton_124_124 sb = (ShineButton_124_124)sender;
                percent = 0;
                int.TryParse(sb.Tag.ToString(), out percent);
                stackOfEnteredInts.Clear();
                lastPressWasPercent = true;
                textBoxHowMany.Text = percent + "%";
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_COMPPERCENT_BUTTON_PERCENT, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="val"></param>
        private void NumericKeyPressed_NOTHROW(int val)
        {
            try
            {
                if (lastPressWasPercent)
                {
                    stackOfEnteredInts.Clear();
                    lastPressWasPercent = false;
                }
                stackOfEnteredInts.Push(val);
                textBoxHowMany.Text = Total.ToString("c");
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_COMPPERCENT_NUMERIC_PRESSED, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearKeyPressed_NOTHROW()
        {
            try
            {
                lastPressWasPercent = false;
                textBoxHowMany.Text = "";
                stackOfEnteredInts.Clear();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_COMPPERCENT_CLEAR_PRESSED, exc);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void OkKeyPressed_NOTHROW()
        {
            try
            {
                if (OkDelegateClicked != null)
                {
                    if (lastPressWasPercent)
                    {
                        OkDelegateClicked((decimal)percent, lastPressWasPercent, compReason, compableSales, isEntireCheck);
                    }
                    else
                    {
                        OkDelegateClicked((decimal)Total, lastPressWasPercent, compReason, compableSales, isEntireCheck);
                    }
                }
                this.Close();
            }
            catch (System.Exception exc)
            {
                Constants.ErrorDispatcher.Notify(Error.EType.E_COMPPERCENT_OK_PRESSED, exc);
            }
        }
    }
}
